package com.photoeditor.demo.ui.widget.autopoll;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.imageloader.GlideApp;
import com.photoeditor.demo.imageloader.ImageLoader;
import com.photoeditor.demo.model.bean.sub.SubBannerBean.BannerBean;

import java.util.List;

public class AutoPollAdapter extends RecyclerView.Adapter<AutoPollAdapter.BaseViewHolder> {

    private Context mContext;
    private List<BannerBean> mData;

    public AutoPollAdapter(Context context, List<BannerBean> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_vip_banner, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        BannerBean bean = mData.get(position % mData.size());
        holder.setImg(bean);
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }


    class BaseViewHolder extends RecyclerView.ViewHolder {

        private ImageView mBanner;
        private TextView mTextView;

        public BaseViewHolder(View itemView) {
            super(itemView);
            mBanner = itemView.findViewById(R.id.iv_banner);
            mTextView = itemView.findViewById(R.id.tv_text);
        }

        private void setImg(BannerBean bean) {
            if (!TextUtils.isEmpty(bean.getBanner())) {
                ImageLoader.loadImage(bean.getBanner(), mBanner);
            } else {
                GlideApp.with(ComponentContext.getContext()).load(bean.getDefaultBannerResId()).into(mBanner);
            }
            mTextView.setText(bean.getTextResId());
        }
    }
}